package com.crm.biz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bouncycastle.crypto.RuntimeCryptoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.BizSysuserCitys;
import com.crm.model.SysDepartment;
import com.crm.model.SysUser;
import com.crm.model.SysUserCitys;
import com.crm.model.SysUserCitysModel;
import com.crm.service.BizSysuserCitysService;
import com.crm.service.SysDepartmentService;
import com.crm.util.ResponseUtil;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/bizSysuserCitys")
public class BizSysuserCitysController {

	@Autowired
	private BizSysuserCitysService bizSysuserCitysService;
	
	@Autowired
	private SysDepartmentService deptService;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request,HttpServletResponse response){
		
		SysUser user = (SysUser) SessionUtils.get(SessionModel.SESSION_USER);
		
		SysDepartment dept = deptService.selectByPrimaryKey(user.getDepartmentId());
		
		if(dept != null){
			
			request.setAttribute("deptName", dept.getFullName());
			
			request.setAttribute("deptId", user.getDepartmentId());
		}
		
		return "configuration/personnelMatch";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public Map<String, Object> getAll(Page<BizSysuserCitys> page, BizSysuserCitys t){
		Map<String, Object> map = new HashMap<>();
		Page<BizSysuserCitys> list = (Page<BizSysuserCitys>) bizSysuserCitysService.selectAll(page, t);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(SysUserCitysModel sysUserCitys, String departmentId,String echelonId){
		List<SysUserCitys> list = sysUserCitys.getSysUserCitys();
		Map<String, Object> map = new HashMap<>();
		
		List<BizSysuserCitys> departmentList = bizSysuserCitysService.selectByDepartment(departmentId);
		if(departmentList.size()>0){
			throw new RuntimeException("当前部门已创建!");
		}
		
		for(SysUserCitys s : list){
			String sysUserId = s.getSysUserId();
			
			//判断是否存在该用户
			BizSysuserCitys bizSysuserCitys = bizSysuserCitysService.selectByPrimaryKey(sysUserId);
			
			if(bizSysuserCitys != null){
				map.put("status", "error");
				map.put("msg", "该用户已存在！");
				return map;
			}
			
			String citysId = s.getCitysId();
			BizSysuserCitys record =new BizSysuserCitys();
			record.setSysuserId(sysUserId);
			record.setCitysId(citysId);
			record.setDepartmentId(departmentId);
			record.setEchelonId(echelonId);
			
			bizSysuserCitysService.insert(record);
		}
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(SysUserCitysModel sysUserCitys, String departmentId, String echelonId){
		
		Map<String, Object> map = new HashMap<>();
		
		bizSysuserCitysService.updateBatch(sysUserCitys, departmentId, echelonId);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		return map;
	}
	
	@RequestMapping("/load")
	@ResponseBody
	public void load(HttpServletResponse response,HttpServletRequest request, Page<BizSysuserCitys> page, BizSysuserCitys t){
		
		SysUser user = (SysUser) SessionUtils.get(SessionModel.SESSION_USER);
		
		t.setDepartmentId(user.getDepartmentId());
		
		if("admin".equals(user.getRealName())){
			t.setDepartmentId("");
		}
		
		//Page<BizSysuserCitys> list = (Page<BizSysuserCitys>) bizSysuserCitysService.selectAllCU(page, t);
		
		Page<Map<String, Object>> list = (Page<Map<String, Object>>)bizSysuserCitysService.selectByGroup(page, t);
		
		ResponseUtil.resultPageMap(response,request,list);
	}
	
	@RequestMapping("/deleteById")
	@ResponseBody
	public Map<String, Object> deleteById(String id){
		
		Map<String, Object> map = new HashMap<>();
		bizSysuserCitysService.deleteByPrimaryKey(id);
		map.put("status", "success");
		map.put("msg", "删除成功！");
		return map;
	}
	@RequestMapping("/getAllOption")
	@ResponseBody
	public Map<String, Object> getAllOption(Page<BizSysuserCitys> page, BizSysuserCitys t){
		Map<String, Object> map = new HashMap<>();
		Page<BizSysuserCitys> list = (Page<BizSysuserCitys>) bizSysuserCitysService.selectAllByDepid(page, t);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
}
